﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;
namespace QuanLyKhachSan
{
    public partial class frmThuTienPhong : DevExpress.XtraEditors.XtraForm
    {
        DataClassesKhachSanDataContext db = new DataClassesKhachSanDataContext(SystemInfo .pub_ConnString );

        public frmThuTienPhong()
        {
            InitializeComponent();
        }
        static int khid;
        static DateTime tu, den;

        static Decimal tongcong = 0, datra = 0;
        static List<thutienphongResult>  chitiet;
        
        private void frmThuTienPhong_Load(object sender, EventArgs e)
        {
            
        }
        void fillKH()
        {
            var khachhang = (from kh in db.THUE_PHONGs
                            where DateTime.Parse(deTuNgay.EditValue.ToString()) <= kh.TP_NGAYBD 
                            && kh.TP_NGAYBD  <= DateTime.Parse(deDenNgay.EditValue.ToString())
                            && DateTime.Parse(deTuNgay.EditValue.ToString()) <= kh.TP_NGAYKT 
                            && kh.TP_NGAYKT <= DateTime.Parse (deDenNgay .EditValue .ToString ()) 
                            select kh.KHACH_HANG).Distinct () ;


            gcKH.DataSource = khachhang;

            //gvChiTiet.s
            

        }

        private void deTuNgay_EditValueChanged(object sender, EventArgs e)
        {
            if (deTuNgay .EditValue != null && deDenNgay .EditValue != null )
            {
                this.fillKH();
            }
        }

        private void deDenNgay_EditValueChanged(object sender, EventArgs e)
        {
            if (deTuNgay.EditValue != null && deDenNgay.EditValue != null)
            {
                this.fillKH();
            }
        }

        private void gvKH_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gvKH .FocusedRowHandle >= 0)
            {
                this.fillChiTiet();
            }
           
        }

        void fillChiTiet()
        {
            khid = int.Parse(gvKH.GetFocusedRowCellValue("KH_ID").ToString());
            DateTime tungay = DateTime.Parse(deTuNgay.EditValue.ToString());
            DateTime denngay = DateTime.Parse(deDenNgay.EditValue.ToString());

            tu = tungay;
            den = denngay;
            
            chitiet = db.thutienphong(khid, tungay, denngay).ToList();

            gcChiTiet.DataSource = chitiet;

           

            var thuephong = (chitiet.Select(t => t.TP_ID).Distinct()).ToList();

            for (int i = 0; i < thuephong.Count; i++)
            {
                //MessageBox.Show(thuephong[i].ToString ());

                //hien thi so tien da tra
                var dathanhtoan = from t in db.DA_THANH_TOANs
                                  where t.TP_ID == int.Parse(thuephong[i].ToString())
                                  select t.DTT_SOTIEN;

                if (dathanhtoan.Count() > 0)
                {
                    var tienphong = dathanhtoan.Sum();
                    datra = datra + tienphong;
                }
                //var tongtien = (from t in db.THUE_PHONGs
                //                where t.TP_ID == int.Parse(thuephong[i].ToString())
                //                select t.TP_TONGCONG).Single();
                //tongcong = tongcong + tongtien;
            }

            for (int i = 0; i < chitiet .Count ; i++)
            {
                Decimal tienphong = 0;
                if (chitiet[i].TP_LOAI_GIA == true  )
                {
                    TimeSpan ngay = DateTime.Today - DateTime.Parse(chitiet[i].CTTP_NGAYBD.ToString().Substring(0, 10));
                    if (ngay.TotalDays < 1)
                    {

                        tienphong = chitiet[i].CTTP_DONGIA;
                    }
                    else
                    {
                        tienphong = int.Parse(ngay.TotalDays.ToString()) * chitiet[i].CTTP_DONGIA;
                    }
                   
                }
                else
                {
                    TimeSpan ngay = DateTime.Now - chitiet[i].CTTP_NGAYBD;
                    tienphong = int.Parse(ngay.TotalHours.ToString()) * chitiet[i].CTTP_DONGIA;
                }
                tongcong = tongcong + tienphong + chitiet[i].DICH_VU + chitiet[i].THUC_AN;

                //MessageBox.Show("Lần thứ " + i.ToString() + " tong cong " + tongcong.ToString());
            }

            txtDaTra.Text = datra.ToString ();
            txtTongCong.Text = tongcong.ToString();
        }

        private void btInHoaDon_Click(object sender, EventArgs e)
        {
            LuuCheckOut();
            frmInHoaDonKhachDoan hoadon = new frmInHoaDonKhachDoan();
            hoadon.thutien = chitiet;
            hoadon.ShowDialog();
        }

        void LuuCheckOut()
        {
            string maphong = "";
            try
                {
                for (int i = 0; i < gvChiTiet.RowCount; i++)
                {
                    
                    var ct = db.CHI_TIET_THUE_PHONGs.Single(c => c.CTTP_ID == Convert.ToInt16(gvChiTiet.GetRowCellValue(i, "CTTP_ID")));

                    ct.CTTP_CHECKOUT = Convert.ToBoolean(gvChiTiet.GetRowCellValue(i, gridColumnCheckOut ));
                    if (ct.CTTP_CHECKOUT == true )
                    {
                         var phong = db.PHONGs.Single(p => p.P_ID == ct.P_ID);
                             phong.TT_ID = 7;
                             maphong += phong.P_MA;
                             maphong += ", ";
                            
                    }
                    db.SubmitChanges();

                    maphong .TrimEnd (' ').TrimEnd (',');
                    
                }
                XtraMessageBox.Show("Check out phòng " + maphong  + " thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
               }
            catch (Exception)
            {

                throw;
            }
        }
       

        private void gvChiTiet_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
        {
            
        }

        private void gvChiTiet_ShowingEditor(object sender, CancelEventArgs e)
        {
            
                if (Convert .ToBoolean ( gvChiTiet .GetFocusedRowCellValue (gridColumn12)) == true
                    && Convert.ToBoolean(gvChiTiet.GetFocusedRowCellValue(gridColumnCheckOut)) == true)
                {
                    e.Cancel = true;
                }
            
        }

       
    }
}